package com.qingcloud.adminbackend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qingcloud.adminbackend.entity.config.SysDictionaryConfig;
import com.qingcloud.adminbackend.entity.user.UserRoleBo;
import com.qingcloud.adminbackend.enums.GridStationCategoryEnum;
import com.qingcloud.adminbackend.mapper.SysDictionaryConfigMapper;
import com.qingcloud.adminbackend.mapper.SysUserRoleMapper;
import com.qingcloud.adminbackend.service.ISysDictionaryConfigService;
import com.qingcloud.adminbackend.utils.DataUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author liuziyong
 * @since 2019-04-12
 */
@Service
public class SysDictionaryConfigServiceImpl extends ServiceImpl<SysDictionaryConfigMapper, SysDictionaryConfig> implements ISysDictionaryConfigService {

    @Resource
    private SysDictionaryConfigMapper sysDictionaryConfigMapper;

    @Resource
    private SysUserRoleMapper sysUserRoleMapper;

    /**
     * 查询子字典配置
     */
    @Override
    public List<SysDictionaryConfig> findChildren(Integer parent) {
        int tempParent = parent;
        //网格化微站统一父编码
        if (GridStationCategoryEnum.ONE.code == parent
                || GridStationCategoryEnum.SIX.code == parent
                || GridStationCategoryEnum.TVOC.code == parent
                || GridStationCategoryEnum.NOISE.code == parent
                || GridStationCategoryEnum.TN.code == parent
                || GridStationCategoryEnum.SIX_MONITOR.code == parent
                || GridStationCategoryEnum.TVOC_MONITOR.code == parent) {
            tempParent = GridStationCategoryEnum.SIX.code;
        }
        return sysDictionaryConfigMapper.findByParent(tempParent);
    }

    @Override
    public List<SysDictionaryConfig> findByType(Integer type) {
        return sysDictionaryConfigMapper.findByType(type);
    }

    @Override
    public List<UserRoleBo> findRoleByUser(String userId) {
        List<UserRoleBo> roleBoList = sysUserRoleMapper.findRoleInfoByUserId(userId);
        return roleBoList;
    }

    @Override
    public SysDictionaryConfig getDictByTypeAndCode(Integer type, String code) {
        LambdaQueryWrapper<SysDictionaryConfig> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(SysDictionaryConfig::getType, type);
        wrapper.eq(SysDictionaryConfig::getCode, code);
        return sysDictionaryConfigMapper.selectOne(wrapper);
    }


    /**
     * 通过类型查询字典keyMap key为code,value为SysDictionaryConfig
     *
     * @param type 类型
     * @return 字典信息
     */
    @Override
    public Map<String, SysDictionaryConfig> getDictMapByType(Integer type) {
        LambdaQueryWrapper<SysDictionaryConfig> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(SysDictionaryConfig::getType, type);
        List<SysDictionaryConfig> sysDictionaryConfigs = sysDictionaryConfigMapper.selectList(wrapper);
        return sysDictionaryConfigs.stream().collect(Collectors.toMap(SysDictionaryConfig::getCode, a -> a, (k1, k2) -> k1));
    }

    /**
     * 通过字典类型 和code字符串集合得到对应的翻译
     *
     * @param type    类型
     * @param codeStr code字符串集合
     * @return code字符串集合得到对应的翻译
     */
    @Override
    public String getDictStr(Integer type, String codeStr) {
        Map<String, SysDictionaryConfig> dictMapByType = getDictMapByType(type);
        return DataUtil.getDictByStr(dictMapByType, codeStr);
    }


}
